package services

import (
	"360/video-srv/basic/config"
	__ "360/video-srv/basic/proto"
	"360/video-srv/modler"
	"context"
	"fmt"
)

type Server struct {
	__.UnsafeVideoServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Login(_ context.Context, in *__.LoginReq) (*__.LoginResp, error) {
	var user = modler.User{
		Phone:    in.Phone,
		Password: in.Password,
	}
	config.DB.Debug().Where("phone = ?", in.Phone).Find(&user)
	code, _ := config.Rdb.Get(config.Ctx, in.Phone).Result()
	if in.Code != code {
		return nil, fmt.Errorf("验证码错误")
	}
	if user.ID == 0 {
		config.DB.Debug().Create(&user)
	}
	return &__.LoginResp{
		UserId: int64(user.ID),
	}, nil
}
